import {Module} from "@nestjs/common";
import {TypeOrmModule} from "@nestjs/typeorm";
import {ConfigService} from "@nestjs/config";
import {Neo4jModule, Neo4jScheme} from "nest-neo4j";
import {Neo4jDao} from "./neo4j.dao";
import {RedisModule} from "./redis/redis.module";
import {MongooseModule} from "@nestjs/mongoose";


@Module({
    imports: [
        /* 连接mysql数据库 */
        TypeOrmModule.forRootAsync({
            useFactory: (configService: ConfigService) => ({
                autoLoadEntities: true,
                type: configService.get<any>("database.type"),
                host: configService.get<string>("database.host"),
                port: configService.get<number>("database.port"),
                username: configService.get<string>("database.username"),
                password: configService.get<string>("database.password"),
                database: configService.get<string>("database.database"),
                autoLoadModels: configService.get<boolean>("database.autoLoadModels"),
                synchronize: false,//configService.get<boolean>('database.synchronize'),
                logging: configService.get("database.logging")
                // entities: [ModalEntity,SpeakerEntity]
            }),
            inject: [ConfigService]
        }),
        /* 连接neo4j数据库 */
        Neo4jModule.forRootAsync({
            useFactory: (configService: ConfigService) => ({
                scheme: configService.get<Neo4jScheme>("neo4j.scheme"),
                host: configService.get<string>("neo4j.host"),
                port: configService.get<string>("neo4j.port"),
                username: configService.get<string>("neo4j.username"),
                password: configService.get<string>("neo4j.password"),
                // logging: {
                //   logger: (level, message) => console.log(`[${level}] ${message}`),
                //   level: "debug"
                // }
            }),
            inject: [ConfigService]
        }),
        /* 连接redis */
        RedisModule.forRootAsync({
            useFactory: (configService: ConfigService) => ({
                host: configService.get<string>("redis.config.host"),
                port: configService.get<number>("redis.config.port"),
                db: configService.get<number>("redis.config.db"),
            }),
            inject: [ConfigService]
        }),
        /* 连接mongoose */
        MongooseModule.forRootAsync({
            useFactory: (configService:ConfigService) => ({
                uri: configService.get<string>("mongoose.uri")
            }),
            inject: [ConfigService]
        })
    ],
    providers: [Neo4jDao],
    exports: [RedisModule, Neo4jDao]
})
export class DatabaseModule {
}
